<?php

/**
 * Class YimeRealNameAuth
 */
class YimeRealNameAuth
{
    /**
     * @var array
     */
    private static $TypeArray = array(
        1 => 'SISE',
        2 => 'ZDNF',
        6 => 'GDSLDL',
    );

    /**
     * @return array
     */
    public static function getTypeArray()
    {
        return self::$TypeArray;
    }

    /**
     * @param int $type
     * @param array $param
     * @throws AlreadyExistException
     * @throws FormatException
     * @throws NotExistException
     * @throws ParaException
     * @return StudentInfo
     */
    public static function doAuth($type = 0, $param = array())
    {
        switch ($type) {
            case 0:
                throw new NotExistException('认证方式未实现');
                break;
            case 1:
                return self::doAuth_SISE($param);
                break;
            case 2:
                return self::doAuth_ZDNF($param);
                break;
            case 6:
                return self::doAuth_GDSLDL($param);
                break;
            default:
                throw new NotExistException('认证方式未实现');
                break;
        }
    }

    /**
     * @param array $param
     * @return StudentInfo
     * @throws AlreadyExistException
     * @throws DatabaseException
     * @throws FormatException
     * @throws ParaException
     */
    private static function doAuth_SISE($param = array())
    {
        $CI=& get_instance();
        $CI->load->library('simplehtmldom_1_5/simple_html_dom');
        if (!isset($param) || !is_array($param))
            throw new ParaException('参数格式不正确');

        if (!isset($param['user_id']) || !isset($param['school_id']) || !isset($param['student_num']) || !isset($param['student_pwd']))
            throw new ParaException('参数不足');

        $user_id     = $param['user_id'];
        $school_id   = $param['school_id'];
        $student_num = $param['student_num'];
        $student_pwd = $param['student_pwd'];

        if (StudentInfo_Model::isSchoolIdAndStudentNumExist($school_id, $student_num)) {
            throw new AlreadyExistException('该学号已被用于认证');
        }

        //-----------获取登录页面
        $loginPageUrl = "http://class.sise.com.cn:7001/sise/";
        $body=StudentAuth::getHtmlCurl($loginPageUrl);
        $matchCount = preg_match_all('<input type=\"hidden\" name=\"(.*)\"  value=\"(.*)\">', $body['body'], $result);
        if ($matchCount !== 1)
            throw new FormatException('教务系统页面结构发生变化，请联系管理员跟进');
        $hiddenInputName  = $result[1][0];
        $hiddenInputValue = $result[2][0];


        //-----------执行登录操作
        $loginApiUrl     = 'http://class.sise.com.cn:7001/sise/login_check_login.jsp';
        $loginPostFields = $hiddenInputName . '=' . $hiddenInputValue . '&username=' . $student_num . '&password=' . $student_pwd;
        $result=StudentAuth::getHtmlCurl($loginApiUrl,$loginPostFields);
        if(!$result){
            return false;
        }else{
            //----------获取学生数据
            $dataPageUrl = 'http://class.sise.com.cn:7001/sise/module/student_schedular/student_schedular.jsp';

            $HtmlDom=StudentAuth::getHtmlCurl($dataPageUrl);
            $get_body=str_get_html($HtmlDom['body']);
            $result=$get_body->find('.style16',0)->plaintext;

            if (!$result) {
                throw new FormatException('认证失败，请检查填写信息是否正确');
            }

            $infoArray=explode(' ',$result);

            if (!$infoArray || !is_array($infoArray) || !$infoArray['0']) {
                throw new FormatException('认证失败，请检查填写信息是否正确');
            }

            $thatStudentInfo = new StudentInfo();

            $thatStudentInfo->setStudentNum($infoArray[1]);
            $thatStudentInfo->setStudentName($infoArray[3]);
            $thatStudentInfo->setGrade($infoArray[5]);
            $thatStudentInfo->setMajor($infoArray[7]);
            $thatStudentInfo->setUserId($user_id);
            $thatStudentInfo->setSchoolId($school_id);

            $thatStudentInfo = StudentInfo_Model::add($thatStudentInfo);

            return $thatStudentInfo;
        }
    }

    /**
     * @param array $param
     * @throws AlreadyExistException
     * @throws ParaException
     * @throws FormatException
     * @internal school 广东水利电力职业技术学院
     * @internal 24407
     * @return StudentInfo
     */
    private static function doAuth_GDSLDL($param = array())
    {
        $CI=& get_instance();
        $CI->load->library('simplehtmldom_1_5/simple_html_dom');
        if (!isset($param) || !is_array($param))
            throw new ParaException('参数格式不正确');

        if (!isset($param['user_id']) || !isset($param['school_id']) || !isset($param['student_num']) || !isset($param['student_pwd']))
            throw new ParaException('参数不足');

        $user_id     = $param['user_id'];
        $school_id   = $param['school_id'];
        $student_num = $param['student_num'];
        $student_pwd = $param['student_pwd'];

        if (StudentInfo_Model::isSchoolIdAndStudentNumExist($school_id, $student_num)) {
            throw new AlreadyExistException('该学号已被用于认证');
        }

        //-----------获取登录页面
        $loginPageUrl = "http://jw1.gdsdxy.cn:81/default2.aspx";
        $body=StudentAuth::getHtmlCurl($loginPageUrl,false,false,false);
        $matchCount = preg_match_all('<input type=\"hidden\" name=\"(.*)\" value=\"(.*)\" />', $body['body'], $result);
        if ($matchCount !== 1)
            throw new FormatException('教务系统页面结构发生变化，请联系管理员跟进');
        $hiddenInputName  = $result[1][0];
        $hiddenInputValue = $result[2][0];

        //-----------执行登录操作
        $loginApiUrl     = 'http://jw1.gdsdxy.cn:81/default2.aspx';
        $loginPostFields = $hiddenInputName . '=' . $hiddenInputValue . '&TextBox1=' . $student_num . '&TextBox2=' . $student_pwd . '&RadioButtonList1=%E5%AD%A6%E7%94%9F&Button1=';

        $result=StudentAuth::getHtmlCurl($loginApiUrl,$loginPostFields,'http://jw1.gdsdxy.cn:81/');
        if(!$result){
            return false;
        }else{
            //----------获取学生数据
            $dataPageUrl = 'http://jw1.gdsdxy.cn:81/xsgrxx.aspx?xh='.$student_num.'&gnmkdm=N121501';

            $HtmlDom=StudentAuth::getHtmlCurl($dataPageUrl,false,'http://jw1.gdsdxy.cn:81/');
            $get_body=str_get_html($HtmlDom['body']);
            if (!$get_body) {
                throw new FormatException('认证失败，请检查填写信息是否正确');
            }
            $stuNum   = $get_body->find('#xh',0)->plaintext;
            $stuName  = $get_body->find('#xm',0)->plaintext;
            $stuGrade = $get_body->find('#lbl_dqszj',0)->plaintext;
            $stuMajor = $get_body->find('#lbl_zymc',0)->plaintext;

            $thatStudentInfo = new StudentInfo();

            $thatStudentInfo->setStudentNum($stuNum);
            $thatStudentInfo->setStudentName($stuName);
            $thatStudentInfo->setGrade($stuGrade);
            $thatStudentInfo->setMajor($stuMajor);
            $thatStudentInfo->setUserId($user_id);
            $thatStudentInfo->setSchoolId($school_id);

            $thatStudentInfo = StudentInfo_Model::add($thatStudentInfo);

            return $thatStudentInfo;
        }
    }

    /**
     * @param array $param
     * @throws AlreadyExistException
     * @throws ParaException
     * @throws FormatException
     * @internal school 中山大学南方学院
     * @internal 12619
     * @return StudentInfo
     */
    private static function doAuth_ZDNF($param = array())
    {
        $CI=& get_instance();
        $CI->load->library('simplehtmldom_1_5/simple_html_dom');
        if (!isset($param) || !is_array($param))
            throw new ParaException('参数格式不正确');

        if (!isset($param['user_id']) || !isset($param['school_id']) || !isset($param['student_num']) || !isset($param['student_pwd']))
            throw new ParaException('参数不足');

        $user_id     = $param['user_id'];
        $school_id   = $param['school_id'];
        $student_num = $param['student_num'];
        $student_pwd = $param['student_pwd'];

        if (StudentInfo_Model::isSchoolIdAndStudentNumExist($school_id, $student_num)) {
            throw new AlreadyExistException('该学号已被用于认证');
        }

        //-----------获取登录页面
        $loginPageUrl = "http://jwxt.nfsysu.cn";
        $body=StudentAuth::getHtmlCurl($loginPageUrl,false,false,false);
        $matchCount = preg_match_all('<input type=\"hidden\" name=\"(.*)\" value=\"(.*)\" />', $body['body'], $result);
        if ($matchCount !== 1)
            throw new FormatException('教务系统页面结构发生变化，请联系管理员跟进');
        $hiddenInputName  = $result[1][0];
        $hiddenInputValue = urlencode($result[2][0]);

        //-----------执行登录操作
        $loginApiUrl     = 'http://jwxt.nfsysu.cn/default2.aspx';
        $loginPostFields = $hiddenInputName . '=' . $hiddenInputValue . '&TextBox1=' . $student_num . '&TextBox2=' . $student_pwd . '&RadioButtonList1=%E5%AD%A6%E7%94%9F&Button1=';

        $result=StudentAuth::getHtmlCurl($loginApiUrl,$loginPostFields,'http://jwxt.nfsysu.cn/');
        if(!$result){
            return false;
        }else{
            //----------获取学生数据
            $dataPageUrl = 'http://jwxt.nfsysu.cn/xsgrxx.aspx?xh='.$student_num.'&gnmkdm=N121501';

            $HtmlDom=StudentAuth::getHtmlCurl($dataPageUrl,false,'http://jwxt.nfsysu.cn/');
            $get_body=str_get_html($HtmlDom['body']);
            if (!$get_body) {
                throw new FormatException('认证失败，请检查填写信息是否正确');
            }
            $stuNum   = $get_body->find('#xh',0)->plaintext;
            $stuName  = $get_body->find('#xm',0)->plaintext;
            $stuGrade = $get_body->find('#lbl_dqszj',0)->plaintext;
            $stuMajor = $get_body->find('#lbl_zymc',0)->plaintext;

            $thatStudentInfo = new StudentInfo();

            $thatStudentInfo->setStudentNum($stuNum);
            $thatStudentInfo->setStudentName($stuName);
            $thatStudentInfo->setGrade($stuGrade);
            $thatStudentInfo->setMajor($stuMajor);
            $thatStudentInfo->setUserId($user_id);
            $thatStudentInfo->setSchoolId($school_id);

            $thatStudentInfo = StudentInfo_Model::add($thatStudentInfo);

            return $thatStudentInfo;
        }
    }
} 